blenza_ifr = null;
var blenza_newwin = 1;
function blenza_submitdata(data, xurl)
{
  if (data == null) return;
  var url = xurl;
  for (i = 0; i < data.length; i++) {
    if (i == 0) {
      url += "?";
    }
    else {
      url += "&";
    }
    var name = data[i].name;
    if (name == "") {
      name = "xyzzy";
    }
    var val = data[i].value;
    url += name;
    url += "=";
    url += escape(val);
  }
  var ifr = document.createElement("iframe");
  blenza_ifr = ifr;
  // comment out next for debugging
  ifr.style.visibility = "hidden";
  ifr.style.position = "absolute";
  ifr.style.top = "100px";
  ifr.style.left = "100px";
  ifr.style.width = "400px";
  ifr.style.height = "400px";
  ifr.style.background = "#00aa00";
  ifr.style.color = "#000000";
  document.body.appendChild(ifr);
  var nf = frames[frames.length - 1];
  var nd = nf.document;
  nd.write("<html>\n<head><title>submit data</title>\n</head>\n<body><span onclick=\"javascript:parent.blenza_ifr.style.visibility=\'hidden\';\">hide this window</span>.\n<ifr" + "ame src=\"" + url + "\"/></ifr" + "ame>\n<p>" + url.replace(/&/g,"&amp;") + "</p></body></html>\n");
  nd.close();
}

function blenza_doSubmit(form, op, action)
{
  if (form.ml_xurl) xurl = form.ml_xurl.value + "/" + action + ".php";
  else if (form.xurl) xurl = form.xurl.value + "/" + action + ".php";
  else xurl = "http://www.blenza.com/linkies/" + action + ".php";
  var data = new Array();
  for (i = 0; i < form.elements.length; i++) {
    var name = form.elements[i].name;
    if (name == "") name = "xyzzy";
    if (name.substring(0,3) == "ml_") name = name.substring(3);
    var val;
    if (form.elements[i].type == "checkbox") {
      if (form.elements[i].checked) {
        val = form.elements[i].value;
      }
      else {
        val = "";
      }
    }
    else {
      val = form.elements[i].value;
    }

    data.push({ "name": name, "value": val });
  }
  blenza_submitdata(data, xurl);
}

function blenza_doEveron(form)
{
  return blenza_doANYon(form, "Everon", "everon_do", "ever-", "ever2-");
}

function blenza_doCommon(form)
{
  return blenza_doANYon(form, "Common", "common_do", "comm-", "comm2-");
}

function blenza_doANYon(form, name, page, p1, p2)
{
  var pi = form.postid.value;
  var div = blenza_elem("title-" + pi);
  if (div != null) {
    form.title.value = div.innerHTML;
  }
  var div = blenza_elem("plink-" + pi);
  if (div != null) {
    form.plink.value = div.innerHTML;
  }
  blenza_doSubmit(form, name, page);
  blenza_switcheroo(p1 + pi, p2 + pi);
  return false;
}

function blenza_switcheroo(off, on)
{
  var ele1 = blenza_elem(off);
  ele1.style.display = "none";
  var ele2 = blenza_elem(on);
  ele2.style.display = "block";
}

function blenza_setNextone(pi, name, url)
{
  var nx = blenza_elem("nextone-" + pi);
  if (nx != null) {
    nx.innerHTML = '<a rel="nofollow" class="blenza-link" href="' + url + '">' + name + '</a>';
  }
}

function blenza_reThumb(a, pi)
{
  var form = document.getElementById("blenza_" + pi);
  if (form == null) {
    alert("Cannot locate form, please report internal error to Mister Linky");
    return false;
  }

  form.action = blenza_xurl + "/thumbwin.php";
  var name;
  if (form.ml_name) name = form.ml_name.value;
  else name = form.name.value;
  if (! blenza_checkName(name)) return false;
  var url;
  if (form.ml_url) url = form.ml_url.value;
  else url = form.url.value;
  if (! blenza_checkUrl(url)) return false;

  a.href = blenza_xurl + "/thumbwin.php?owner=" + escape(form.ml_owner.value) + "&postid=" + escape(pi) + "&name=" + escape(name) + "&url=" + escape(url);

  return true;
}

function blenza_doEnter(form)
{
  var name;
  if (form.ml_name) {
    name = form.ml_name.value;
  }
  else {
    name = form.name.value;
  }
  var url;
  if (form.ml_url) {
    url = form.ml_url.value;
  }
  else {
    url = form.url.value;
  }
  if (! blenza_checkName(name)) {
    return false;
  }
  if (! blenza_checkUrl(url)) {
    return false;
  }
  if (form.ml_src.value != "thumb")
  blenza_doSubmit(form, "Entry", "entry_do");
  var pi;
  if (form.ml_postid) {
    pi = form.ml_postid.value;
  }
  else {
    pi = form.postid.value;
  }
  blenza_setNextone(pi, name, url);
  blenza_switcheroo("theform-" + pi, "theform2-" + pi);

  if (form.ml_src.value == "thumb") {
    form.target = "_blank";
    form.method = "POST";
    form.action = blenza_xurl + "/thumbwin.php";
    var name;
    if (form.ml_name) name = form.ml_name.value;
    else name = form.name.value;
    var url;
    if (form.ml_url) url = form.ml_url.value;
    else url = form.url.value;
    var popurl = blenza_xurl + "/thumbwin.php?owner=" + escape(form.ml_owner.value) + "&postid=" + escape(pi) + "&name=" + escape(name) + "&url=" + escape(url);
    var popwin = window.open(popurl, "blenza_linkypop", "height=400,width=480,top=100,left=100,location=no,menubar=no,resizable=yes,scrollbars=yes,status=yes,toolbar=no");
    if (popwin == null) return true;
    else return false;
  }

  return false;
}

function blenza_checkName(name)
{
  if (name.length == 0) {
    alert(bzloc_checkname);
    return false;
  }
  if (name.length > blenza_opt_namemax) {
    alert(bzloc_namelength + " " + blenza_opt_namemax);
    return false;
  }
  return true;
}

function bz_starts(str, pre)
{ return (str.substring(0, pre.length) == pre); }

function blenza_checkUrl(url)
{
  if (url.length == 0) {
    alert(bzloc_checkurl);
    return false;
    return true;
  }
  if (url.length > blenza_opt_linkmax) {
    alert(bzloc_urllength + " " + blenza_opt_linkmax);
    return false;
    return true;
  }
  if (!bz_starts(url, "http://") && !bz_starts(url, "https://")) {
    alert(bzloc_urlprefix);
    return false;
  }
  if (url == "http://www.") {
    alert(bzloc_checkurl);
    return false;
  }
  return true;
}

function blenza_elem(id)
{
  return document.getElementById(id);
}

function blenza_display(xurl, owner, pi)
{
  var popwin = window.open(xurl + "/display.php?owner=" + owner + "&postid=" + pi, "blenza_linkypop", "height=360,width=400,top=100,left=100,location=no,menubar=no,resizable=yes,scrollbars=yes,status=yes,toolbar=no");
  if (popwin != null) {
    return;
  }
}

function blenza_getthumb(owner, pi, secret)
{
  var popurl = blenza_thumbGetter + "getthumb.php?owner=" + owner + "&postid=" + pi + "&secret=" + secret + "&init=true";
  var popwin = window.open(popurl, "blenza_linkypop", "height=400,width=480,top=100,left=100,location=no,menubar=no,resizable=yes,scrollbars=yes,status=yes,toolbar=no");
/*
  if (popwin == null) {
    if (confirm("We were unable to pop-up the thumbnail window. Do you wish to go to the thumbnail page in this window?")) document.location = popurl;
    return;
  }
*/
  if (popwin != null)
  setTimeout(function(){ blenza_getthumb2(popwin, popurl); }, 3000);
}

function blenza_getthumb2(popwin, popurl)
{
  try {
    if (popwin == null) return;
    if (popwin.innerHeight != null) return;
  }
  catch (err) {
    return;
  }
}

var blenza_whoName = null;
var blenza_whoEmail = null;
var blenza_whoURL = null;
var blenza_user = null;
var blenza_xurl = null;
var blenza_showredx = true;
var blenza_color = null;

var blenza_thumbGetter = null;
var blenza_thumbExternal = null;
var blenza_thumbRoot = new Array();
var blenza_thumbApprove = null;

var blenza_border = "#000";
var blenza_gray = "#888";

var blenza_meme_unset = new Array();
var blenza_meme_id = new Array();
var blenza_meme_name = new Array();
var blenza_meme_text = new Array();
var blenza_meme_shared = new Array();

var blenza_ord = 0;

function blenza_setwho(n, e, u)
{
  blenza_whoName = n;
  blenza_whoEmail = e;
  blenza_whoURL = u;
}

function blenza_setuser(v)
{
  blenza_user = v;
}

function blenza_setredx(v)
{
  blenza_showredx = v;
}

function blenza_setxurl(v)
{
  blenza_xurl = v;
}

function blenza_setcolor(v)
{
  blenza_color = v;
}

function blenza_setthumbs(pi, g, e, r, a)
{
  blenza_thumbGetter = g;
  blenza_thumbExternal = e;
  blenza_thumbRoot[pi] = r;
  blenza_thumbApprove = (a > 0);
}

function blenza_setmeme(pi, id, name, text, shared)
{
  blenza_meme_unset[pi] = false;
  blenza_meme_id[pi] = id;
  blenza_meme_name[pi] = name;
  blenza_meme_text[pi] = text;
  blenza_meme_shared[pi] = shared;
}

function blenza_unsetmeme(pi)
{
  blenza_meme_unset[pi] = true;
  blenza_meme_id[pi] = null;
  blenza_meme_name[pi] = null;
  blenza_meme_text[pi] = null;
  blenza_meme_shared[pi] = null;
}

var blenza_expiration = null;
function blenza_setExpires(str)
{
  blenza_expiration = str;
}
function blenza_dispExpires()
{
  if (blenza_expiration == null) return;
  document.write("<p>" + bzloc_expiry + ": " + blenza_expiration + ".</p>");
}

function blenza_dispexp(owner, pi, cols)
{
  if (cols==null) cols=3;
  document.write('<p style="border: 2px solid ' + blenza_border + '; text-align: center; padding: 4px; color: ' + blenza_border + ';"><a rel="nofollow" target="_blank" href="' + blenza_xurl + '/export.php?owner=' + owner + '&postid=' + pi + '&cols=' + cols + '">' + bzloc_owner + ': ' + bzloc_export + '</a>.  <a rel="nofollow" target="_blank" href="' + blenza_xurl + '/options_do.php?section=export&submit=Hide">' + bzloc_disablemsg + '</a>.  <a target="_blank" href="http://www.misterlinky.net/blog/?p=10">(' + bzloc_whatthis + ')</a>');
}

function blenza_cktool(xurl)
{
  var popwin = window.open("http://myanmarstudentlibrary.blogspot.com/", "blenza_cktool", "height=360,width=400,top=100,left=100,location=no,menubar=no,resizable=yes,scrollbars=yes,status=yes,toolbar=no");
  return (popwin == null);
}

function blenza_isUpper(s)
{
  if ((s >= 'A') && (s <= 'Z')) return true; else return false;
}

function blenza_isLower(s)
{
  if ((s >= 'a') && (s <= 'z')) return true; else return false;
}

function blenza_isPunc(s)
{
  if (((s >= 'A') && (s <= 'Z')) || ((s >= 'a') && (s <= 'z')) || ((s >= '0') && (s <= '9')) || (s <= ' ')) return false; else return true;
}

var blenza_ntu = new Array();
var blenza_bycol = true;

function blenza_linkies_list(src, owner, pi, secret, cols, data)
{
  blenza_ord++;

  var closed = (blenza_expiration != null)
    && (blenza_expiration.substr(0, 3) != "in ");

  if (closed) {
    if (data[data.length-1][0] < 0) {
      data.pop();
    }
  }

  for (var i = 0; i < data.length; i++) {
    var ntukey = data[i][1]+"|"+data[i][2];
    if (((src == "thumb") && (data[i][4] == 0)) || blenza_ntu[ntukey]) {
      for (var j = i+1; j < data.length; j++) data[j-1] = data[j];
      data.pop();
      i--;
    }
    else blenza_ntu[ntukey] = 1;
  }
  var len = data.length;

  var rows = Math.floor((len + cols-1) / cols);
  var cw;
  if (cols == 1) cw = 95;
  else if (cols == 2) cw = 45;
  else if (cols == 3) cw = 28;
  var iis = new Array();
  for (var i = 0; i < cols; i++) iis[i] = i * rows;
  document.write('<div class="blenza-top"><table class="blenza-table" width="100%"><tr>');
  for (var i = 0; i < rows * cols; i++) {
    if (((i % cols) == 0) && (i != 0)) {
      document.write('</tr><tr>');
    }
    var col = i % cols;
    if (blenza_bycol) ii = iis[col]++;
    else ii = i;
    var n = ii + 1;
    if (ii >= len) {
      document.write('<td class="blenza-td" width="5%" align="left" valign="top">');
      document.write("&nbsp;");
      document.write('</td>');
      document.write('<td class="blenza-td" width="' + cw + '%" align="left" valign="top">');
      document.write("&nbsp;");
      document.write('</td>');
      continue;
    }
    if (closed && (data[ii][0] < 0)) continue;
    var name = data[ii][1];
    document.write('<td class="blenza-td" width="5%" align="left" valign="top">');
    document.write('<span class="blenza-link">' + n + '.</span>');
    document.write('</td>');
    document.write('<td class="blenza-td" width="' + cw + '%" align="left" valign="top">');
    document.write('<span class="blenza-link">');
    if ((name != null) && (name.substr(0,1) == "&")) name = " " + name;
    if (name != null) {
      var newname = "";
      var inamp = false;
      for (var j = 0; j < name.length; j++) {
        var cj = name.substr(j, 1);
        if (j == 0) {
          newname += cj;
          continue;
        }
        if (cj == '&') {
          inamp = true;
          newname += cj;
          continue;
        }
        if (inamp) {
          if (cj == ';') inamp = false;
          newname += cj;
          continue;
        }
        var cp = name.substr(j-1, 1);
        if ((blenza_isUpper(cj) && blenza_isLower(cp))
            || (!blenza_isPunc(cj) && blenza_isPunc(cp))) {
          var ksp = false;
          for (k = j; (k < name.length) && (k <= j+2); k++) {
            var ck = name.substr(k, 1);
            if (ck == ' ') ksp = true;
          }
          for (k = j-1; (k >= 0) && (k >= j-3); k--) {
            var ck = name.substr(k, 1);
            if (ck == ' ') ksp = true;
          }
          if (!ksp) newname += "<span class='blenza-label' style='font-size: 1px;'> </span>";
        }
        newname += cj;
      }
      name = newname;
    }
    if (name == blenza_whoName) {
      name = "<b>" + name + "</b>";
    }
    if (data[ii][0] < 0) {
      if (src == "thumb") {
        document.write('<i' + 'mg border="0" height="75" width="75" src="' + blenza_xurl + '/images/thisisyou');
        if (blenza_color != null)
          document.write('-' + blenza_color);
        document.write('.png' + '">');
        document.write('<br>');
      }
      document.write('<span class="blenza-label" id="nextone-' + pi + '" style="display: inline">');
      if (data.length == 1) {
        document.write(bzloc_youfirst);
      }
      else {
        document.write(bzloc_younext);
      }
      document.write("!</span>");
    }
    else if (data[ii][2]) {
      document.write('<a rel="nofollow" href="' + data[ii][2] + '"');
      if (blenza_newwin == 1) document.write(' target="_blank"');
      document.write('>');
      if (src == "thumb") {
        var imgurl;
        if ((data[ii][4] < 0) && !blenza_thumbApprove) imgurl = blenza_xurl + '/images/pending.png';
        else imgurl = blenza_thumbExternal + blenza_thumbRoot[pi] + "/" + data[ii][0] + ".png";
        document.write('<i' + 'mg border="0" height="75" width="75" src="' + imgurl + '">');
        document.write('<br>');
      }
      if (src == "thumb") document.write('<div style="height:2.5em;overflow:hidden" onmouseover="this.style.overflow=\'visible\'" onmouseout="this.style.overflow=\'hidden\'">');
      document.write(name);
      document.write('</a>');
      // close div below
    }
    else {
      document.write(name);
    }
    if (data[ii][3]) {
      document.write(' <a rel="nofollow" href="' + blenza_xurl + '/mylink.php?postid=' + pi + '&entrid=' + data[ii][0] + '&secret=' + data[ii][3] + '" target="_blank"><i' + 'mg style="border: 0px; padding: 0px;" border="0" align="absmiddle" src="' + blenza_xurl + '/images/user.png"></a>');
    }
    if ((blenza_user == owner) && (data[ii][0] >= 0)) {
      document.write(' <a rel="nofollow" href="' + blenza_xurl + '/adlink.php?postid=' + pi + '&entrid=' + data[ii][0] + '" target="_blank"><i' + 'mg style="border: 0px; padding: 0px;" border="0" align="absmiddle" src="' + blenza_xurl + '/images/redx.png"></a>');
    }
    if (data[ii][2] && (src == "thumb")) document.write('</div>');
    document.write('</span>');
  }
  document.write('</td></tr></table></div>');
}

function blenza_linkies_wrap1()
{
  document.write('<div class="blenza-wrapper">');
}

function blenza_linkies_wrap2()
{
  document.write('</div>');
}

function blenza_linkies_ini(src, owner, pi, secret, cols, data, mode)
{
  var pv = document.getElementById("preview-" + pi);
  if (pv != null) pv.style.display = "none";
  document.write('<div class="blenza-box" id="theform-' + pi + '">');
  document.write('<span class="blenza-intro"><a id="bztitle" target="_blank" href="http://myanmarstudentlibrary.blogspot.com/"><b>Myanmar Student Library&#39;s Magical Widgets</b></a><br/>');
  if (mode == "link") document.write('<a target="_blank" href="' + blenza_xurl + '/links.php?owner=' + owner + '&postid=' + pi + '&src=' + src + '">');
  if (blenza_meme_name[pi]) document.write(bzloc_ifparticip + ' <i>' + blenza_meme_name[pi] + '</i>');
  else document.write(bzloc_toaddlink);
  document.write(", ");
  if (mode == "box") {
    if (src == "thumb") document.write(bzloc_thumb2);
    else document.write(bzloc_siteusing2);
  }
  if (mode == "link") document.write(bzloc_siteusing3 + '</a>.');
}

function blenza_linkies_fini(src, owner, pi, secret, cols, data)
{
  if (blenza_meme_shared[pi] > 0) {
    document.write('<br/><a rel="nofollow" target="_blank" href="' + blenza_xurl + '/subs_subscr.php?id=' + blenza_meme_id[pi] + '">' + bzloc_sharedmeme + '</a>');
  }
  document.write('</span>');
  document.write('</div>');
  if (blenza_meme_text[pi]) document.write('<div class="blenza-meme">' + blenza_meme_text[pi] + '</div>');
}

function blenza_linkies_box(src, owner, pi, secret, cols, data, mode)
{
  if (blenza_meme_unset[pi]) {
    var pv = document.getElementById("preview-" + pi);
    if (pv != null) pv.style.display = "none";
    return;
  }
  blenza_linkies_ini(src, owner, pi, secret, cols, data, "box");
  document.write('<table border="0" align="center"><form style="display: inline;" id="blenza_' + pi + '" name="blenza_' + pi + '" ');
  if (mode == "magic") {
    document.write('onsubmit="return blenza_doEnter(this);"');
  }
  if (src == "thumb") {
    button = bzloc_next;
    afterComment = "<a onclick=\"return blenza_reThumb(this, '" + pi + "')\" target=\"_blank\" href=\"\">" + bzloc_thumbnext + "</a><br>" + bzloc_comment;
  }
  else {
    button = bzloc_enter;
    afterComment = bzloc_comment;
  }
  spanCktool = '<br><a id="bzcktool" target="_blank" href="http://www.facebook.com/pages/Student-Library/128873167268998" style="cursor:pointer;text-decoration:none" onclick="return blenza_cktool(blenza_xurl);">' + bzloc_cktool + '</a>';
  document.write('><input type="hidden" name="ml_xurl" value="' + blenza_xurl + '"/><input type="hidden" name="ml_owner" value="' + owner + '"/><input type="hidden" name="ml_secret" value="' + secret + '"/><input type="hidden" name="ml_postid" value="' + pi + '"/><input type="hidden" name="ml_meme" value="' + blenza_meme_id[pi] + '"/><input type="hidden" name="ml_src" value="' + src + '"/><input type="hidden" name="ml_timezone" value="' + new Date().getTimezoneOffset() + '"/><input type="hidden" name="ml_referer" value="' + document.referrer + '"/><tr><td class="blenza-tdl" width="25%" align="right" valign="middle"><span class="blenza-label">' + bzloc_yourname + ':</span>&nbsp;</td><td class="blenza-tdm" width="50%" align="center" valign="middle"><span class="blenza-input-wrap"><input size="32" class="blenza-input" tabindex="' + ((blenza_ord*3)+101) + '" name="ml_name" value="' + blenza_whoName + '"/></span></td><td class="blenza-tdr" rowspan="2" width="25%" align="left" valign="middle"><span class="blenza-button-wrap">&nbsp;<input class="blenza-button" tabindex="' + ((blenza_ord*3)+103) + '" type="submit" value="  ' + button + '  "/></span></td></tr><tr><td class="blenza-tdl" width="25%" align="right" valign="middle"><span class="blenza-label">' + bzloc_yoururl + ':</span>&nbsp;</td><td class="blenza-tdm" width="50%" align="center" valign="middle"><span class="blenza-input-wrap"><input size="32" class="blenza-input" tabindex="' + ((blenza_ord*3)+102) + '" name="ml_url" value="' + blenza_whoURL + '"/></span></td></tr></form></table><span class="blenza-after">' + bzloc_comment + spanCktool + '</span></div><div class="blenza-box" id="theform2-' + pi + '" style="display: none;"><span class="blenza-after">' + afterComment + spanCktool);
  if ((src != "easy") && (src != "thumb")) {
    document.write("<br/>" + bzloc_caching);
  }
  document.write('</span>');
  blenza_linkies_fini(src, owner, pi, secret, cols, data);
}

function blenza_linkies(src, owner, pi, secret, cols, data)
{
  if (pi == "^Preview") data = previewData;
  blenza_linkies_wrap1();
  blenza_linkies_list(src, owner, pi, secret, cols, data);
  if ((src == 'comm') || (src == 'disp')) return;
  blenza_linkies_box(src, owner, pi, secret, cols, data, "magic")
  blenza_linkies_wrap2();
}
